//
//  UserViewController.m
//  Product_NEWS
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 FCH. All rights reserved.
//

#import "UserViewController.h"
#import "FavoriteController.h"
#import "UserCell.h"
#import "NightModeHandle.h"
#import "SuggestViewController.h"
#import "DataHandle.h"
static CGFloat ImageHeight  = 300 ;
;
@interface UserViewController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
@property (nonatomic, retain) UIImageView *imgProfile;
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UITableView *myTableView;

@end

@implementation UserViewController
{
    CGFloat caChe;
}



- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBarHidden = YES;
    [super viewWillAppear:animated];
    CGRect bounds = self.view.bounds;
    self.scrollView.frame = bounds;

    //图片缓存
    caChe = [[SDImageCache sharedImageCache] checkTmpSize];
  
    
    [self.myTableView reloadData];

}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self initUI];
  
}

-(void)initUI
{
    //图片
    self.navigationController.navigationBarHidden = YES;
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.delegate = self;
    self.scrollView.backgroundColor = [UIColor clearColor];
    self.scrollView.contentSize = CGSizeMake([[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height );
    [self.view addSubview:self.scrollView];
    UIImage *image = [UIImage imageNamed:@"logo"];
    self.imgProfile = [[UIImageView alloc] initWithImage:image];
    self.imgProfile.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, ImageHeight  );
//    self.imgProfile.userInteractionEnabled = YES;
    
    [self.view addSubview:self.imgProfile];
    
    
    //tableView
    self.myTableView =[[[UITableView alloc] initWithFrame:CGRectMake(0, ImageHeight - 35 * SCREEN_HEIGHT, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped] autorelease];
   
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.scrollView addSubview:_myTableView];
    
    [self.myTableView registerClass:[UserCell class] forCellReuseIdentifier:@"cell"];
    
        self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat yOffset   = self.scrollView.contentOffset.y;
    
    if (yOffset < 0) {
        
        CGFloat factor = ((ABS(yOffset)+ImageHeight)*[[UIScreen mainScreen] bounds].size.width)/ImageHeight;
        CGRect f = CGRectMake(-(factor-[[UIScreen mainScreen] bounds].size.width)/2, 0, factor, ImageHeight+ABS(yOffset));
        self.imgProfile.frame = f;
    } else {
        CGRect f = self.imgProfile.frame;
        f.origin.y = -yOffset ;
        self.imgProfile.frame = f;
    }
}


#pragma mark -- tableDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UserCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0) {
        cell.title.text = @"我的收藏";
         cell.imageView.image = [UIImage imageNamed:@"我的收藏"];
        
    }
   
        if (indexPath.row == 1) {
        cell.title.text = @"清除缓存";
        cell.imageView.image = [UIImage imageNamed:@"清除缓存"];
        cell.caCheLabel.text = caChe>= 1 ? [NSString stringWithFormat:@"%.2fM",caChe] : [NSString stringWithFormat:@"%.2fK",caChe * 1024];
        [cell addSubview:cell.caCheLabel];

    }
    if (indexPath.row == 2) {
        cell.title.text = @"意见反馈";
          cell.imageView.image = [UIImage imageNamed:@"意见"];
        
    }
    if (indexPath.row == 3) {
        cell.title.text = @"免责声明";
        cell.imageView.image = [UIImage imageNamed:@"免责"];
        
        
        
    }
    if (indexPath.row == 4) {
        cell.title.text = @"关于作者";
        cell.imageView.image = [UIImage imageNamed:@"关于我们"];
    }
    return cell;
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    
    if (indexPath.row == 0) {
        FavoriteController *favoriteController = [[FavoriteController alloc]init];        
            [self.navigationController pushViewController:favoriteController animated:YES];
        [favoriteController release];
        
        
    }
        if (indexPath.row == 1) {
//缓存
        [self clearCache];
    }
    if (indexPath.row == 2) {
        [self suggest];
    }
    if (indexPath.row == 3) {
       
    }
    if (indexPath.row == 4) {
    
        
    }

}



#pragma mark  -- 清除缓存


-(void)clearCache
{
    if (caChe <= 0.0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"暂无缓存" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
        [alert release];
        return;
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否清除" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    [alert show];
    [alert release];
    
}



-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        MBProgressHUD *hud = [[MBProgressHUD alloc]init];
        [self.view addSubview:hud];
        hud.labelText = @"缓存清理中";
        
        hud.mode = MBProgressHUDModeDeterminate;
        hud.dimBackground = YES;

        
        [hud showAnimated:YES whileExecutingBlock:^{
            while (hud.progress < 1.0) {
                hud.progress += 0.01;
                [NSThread sleepForTimeInterval:0.02];
            }

            hud.labelText = @"清理完成";
            
        } completionBlock:^{
          //清理图片缓存
            [[SDImageCache sharedImageCache]clearDisk];
            [[SDImageCache sharedImageCache] clearMemory];
            caChe = 0.0;
            [self.myTableView reloadData];
            [hud removeFromSuperview];
            
        }];
    }
}

-(void)suggest
{
    
    SuggestViewController *vc = [[SuggestViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];

    
}
//#pragma mark - 去处多余的分割线
- (void)DropCellLine {
    UIView *footerView = [[UIView alloc] init];
    footerView.backgroundColor = [UIColor clearColor];
    self.myTableView.tableFooterView = footerView;
    [footerView release];


}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
